package com.edu.hre.struts.action.leave;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.entity.HrePageInfo;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.DynamicConfig;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.db.pojo.HreEmployee;
import com.edu.hre.db.pojo.HreLeaveRequest;
import com.edu.hre.db.pojo.HreLeaveType;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.pojo.HreUserRoleScreen;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.action.admin.ConfigurationListAction;
import com.edu.hre.struts.form.admin.CustomerListForm;
import com.edu.hre.struts.form.leave.LeaveRequestForm;
import com.edu.hre.struts.form.leave.LeaveRequestListForm;
import com.edu.hre.struts.form.leave.LeaveRequestUpdateForm;

public class LeaveRequestListAction extends DispatchAction {
	
	private static final Logger logger = Logger.getLogger(LeaveRequestListAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		HreUserRoleScreen userRoleScreen = (HreUserRoleScreen) request.getSession().getAttribute(RequestUtils.Attribute.HreUserRoleScreen);
		HreUser user = (HreUser) request.getSession().getAttribute(RequestUtils.Attribute.USER_INFO);
		LeaveRequestListForm beanForm = (LeaveRequestListForm) form;
		int currentPage = RequestUtils.getParameterInt(request, "page", 1);
		int pageSize = CommonUtils.parseInt(DynamicConfig.getConfig(DynamicConfig.PAGE_SIZE), 20);
		if (request.getParameter("btnDelete") != null) {
			List <LeaveRequestForm> listObjs = beanForm.getListObjs();
			for(int i = 0; listObjs != null && i < listObjs.size(); i++){
				LeaveRequestForm tmp = (LeaveRequestForm) listObjs.get(i);
				CommonUtils.println(tmp.isSelected() + "|" + tmp.getApproved());
				if(tmp.isSelected() && tmp.getApproved() == null){
					baseService.deleteFirstPojo(tmp.getId(), HreLeaveRequest.class);
				} 
			}
		}
		beanForm.setCanAdd(userRoleScreen.isCanCreate());
		beanForm.setCanDelete(userRoleScreen.isCanDelete());
		String sortBy = RequestUtils.getParameterStr(request, "sort", null);
		if (sortBy != null) { 
			beanForm.setAsc(!beanForm.isAsc());
			CommonUtils.sortCollection(beanForm.getListObjs(), beanForm.isAsc(), sortBy);
			return mapping.findForward("leaveRequestList");
		}
		
		int totalItems = 0;
		List <HreLeaveRequest> listHreLeaveRequest = null;
		if(beanForm.getFromDate() != null && beanForm.getToDate() != null){
			totalItems = baseService.countByColumns(new String[]{"empNum", "firstLeave", "lastLeave"},
					new int[]{Compare.EQUAL, Compare.GREATER_EQUAL, Compare.LESS_EQUAL},
					new Object[]{user.getEmpNumber(), beanForm.getFromDate(), beanForm.getToDate()}, "id", HreLeaveRequest.class);
			listHreLeaveRequest = baseService.getPojosAnd(new String[]{"empNum", "firstLeave", "lastLeave"},
					new int[]{Compare.EQUAL, Compare.GREATER_EQUAL, Compare.LESS_EQUAL},
					new Object[]{user.getEmpNumber(), beanForm.getFromDate(), beanForm.getToDate()}, new String[]{"dateCreated"}, new Boolean[]{true},  pageSize, pageSize * (currentPage - 1), HreLeaveRequest.class);
		} else if(beanForm.getFromDate() != null){
			totalItems = baseService.countByColumns(new String[]{"empNum", "firstLeave"},
					new int[]{Compare.EQUAL, Compare.GREATER_EQUAL},
					new Object[]{user.getEmpNumber(), beanForm.getFromDate()}, "id", HreLeaveRequest.class);
			listHreLeaveRequest = baseService.getPojosAnd(new String[]{"empNum", "firstLeave"},
					new int[]{Compare.EQUAL, Compare.GREATER_EQUAL},
					new Object[]{user.getEmpNumber(), beanForm.getFromDate()}, new String[]{"dateCreated"}, new Boolean[]{true},  pageSize, pageSize * (currentPage - 1), HreLeaveRequest.class);
		} else if(beanForm.getToDate() != null){
			totalItems = baseService.countByColumns(new String[]{"empNum", "lastLeave"},
					new int[]{Compare.EQUAL, Compare.LESS_EQUAL},
					new Object[]{user.getEmpNumber(), beanForm.getToDate()}, "id", HreLeaveRequest.class);
			listHreLeaveRequest = baseService.getPojosAnd(new String[]{"empNum", "lastLeave"},
					new int[]{Compare.EQUAL, Compare.LESS_EQUAL},
					new Object[]{user.getEmpNumber(), beanForm.getToDate()}, new String[]{"dateCreated"}, new Boolean[]{true},  pageSize, pageSize * (currentPage - 1), HreLeaveRequest.class);
		} else {
			totalItems = baseService.countByColumns(new String[]{"empNum"}, new int[]{Compare.EQUAL}, new Integer[]{user.getEmpNumber()}, "id", HreLeaveRequest.class);
			listHreLeaveRequest = baseService.getPojos("empNum", Compare.EQUAL, user.getEmpNumber(), new String[]{"dateCreated"}, new Boolean[]{true},  pageSize, pageSize * (currentPage - 1), HreLeaveRequest.class);
		}
		List<HreLeaveType> listLeaveType = baseService.getPojos(null, -1, null, null, null, 0, 0, HreLeaveType.class);
		List<LeaveRequestForm> listObjs = new ArrayList<LeaveRequestForm>();
		for(HreLeaveRequest tmp : listHreLeaveRequest){
			LeaveRequestForm obj = new LeaveRequestForm(tmp.getId(), tmp.getEmpNum(), tmp.getFirstLeave(), tmp.getLastLeave(), tmp.getSupervisorNum(), tmp.getApproved(), tmp.getLeaveTypeId(), tmp.getEmpComment(), tmp.getSupComment());
			HreEmployee supervisor = baseService.getPojo(obj.getSupervisorNum(), HreEmployee.class);
			if(supervisor != null){
				obj.setSupervisorFullName(supervisor.getEmpLastname() + (supervisor.getEmpMiddleName() == null ? " " : " " + supervisor.getEmpMiddleName() + " ") + supervisor.getEmpFirstname());
				obj.setLeaveTypeName(getNameLeaveType(listLeaveType, obj.getLeaveTypeId()));
				listObjs.add(obj);
			} else {
				totalItems --;
			}
		}
		ArrayList<HrePageInfo> allPages = CommonUtils.getDisplayingPages(currentPage, CommonUtils.getTotalPage(totalItems));
		beanForm.setTotalPage(CommonUtils.getTotalPage(totalItems));
		beanForm.setListPages(allPages);
		beanForm.setTotalItems(totalItems); 
		
		beanForm.setListObjs(listObjs); 
		return mapping.findForward("leaveRequestList");
	}
	
	private String getNameLeaveType(List <HreLeaveType> listObjs, int id){
		for(int i = 0; listObjs != null && i < listObjs.size(); i++){
			HreLeaveType tmp = listObjs.get(i);
			if(id == tmp.getId()){
				return tmp.getName();
			}
		}
		return null;
	}
}
